package collector;

import parser.CodeReviewToken;
import parser.TextLine;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author dotekien
 *         Date: Mar 13, 2006
 *         Time: 5:32:02 PM
 */
public class Statement {
    private static final AtomicInteger s_numofStat = new AtomicInteger(0);

    private final int id; //for matrix
    private final FileObj file;     //belong to only one file
    //    private ArrayList<Chunk> chain = new ArrayList<Chunk>();    //belong to one more chain
    private String dumpedAST;

    //    private int sTokenIndex; //start token
    //    private int eTokenIndex; //end token
    private final Range tokenRange;

//    private int numOfDup = 0;

//    private ArrayList<AST> trees;

    public Statement(FileObj f, TextLine tl) {
        id = s_numofStat.getAndIncrement();

        file = f;
        file.addStatement(this);

//        sTokenIndex = tl.getsTokenIndex();
//        eTokenIndex = tl.geteTokenIndex();
        tokenRange = tl.getTokenRange();

        dumpedAST = tl.getString();
    }

    static void resetNumOfStat() {
        s_numofStat.set(0);
    }

    public int getId() {
        return id;
    }

    public FileObj getFile() {
        return file;
    }

//    public ArrayList<Chunk> getChain() {
//        return chain;
//    }

//    public void setChain(ArrayList<Chunk> chain) {
//        this.chain = chain;
//    }

    public String getDumpedAST() {
        return dumpedAST;
    }

//    public void setDumpedAST(String dumpedAST) {
//        this.dumpedAST = dumpedAST;
//    }

    public int getsTokenIndex() {
        return tokenRange.start;
    }

    public int geteTokenIndex() {
        return tokenRange.end;
    }

//    public int getNumOfDup() {
//        return numOfDup;
//    }

//    public void setNumOfDup(int numOfDup) {
//        this.numOfDup = numOfDup;
//    }

//    public ArrayList<AST> getTrees() {
//        return trees;
//    }

//    public void setTrees(ArrayList<AST> trees) {
//        this.trees = trees;
//    }

    public boolean equals(Statement other) {
        return dumpedAST.equals(other.dumpedAST);
    }


    public String toString() {
        CodeReviewToken first = file.getTokens().get(tokenRange.start);
        CodeReviewToken end = file.getTokens().get(tokenRange.end);
        StringBuffer sb = new StringBuffer();
        sb.append("[").append(id).append(": ");
        sb.append(dumpedAST).append(", ");
        sb.append(first.getLine()).append(", ");
        sb.append(end.getLine()).append(", ");
        sb.append(first.getStartIndex()).append(", ");
        sb.append(end.getEndIndex()).append("]");

        return sb.toString().intern();
    }
}
